<!--
* description: 财务管理-奖励金使用比例设置
* author: wangjuan
* createdDate: 2021-3-4
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-show-select="false"
      :dynamic-is-init-table="true"
    />
  </div>
</template>
<script>

import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import { setDynamicFieldsData } from '@/utils/dataOperation'
import Vue from 'vue'
import api from '@/api/pa/financialManagement'

export default {
  name: 'RewardUsageRatioSetting',

  components: {
    OneTableTemplate
  },

  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },



  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      // 网格查询API配置对象
      apiConfig: api.rewardUsageQueryByPage,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'preservation',
          clickEvent: () => this.save(),
          text: this.$t('sys.button.save'),
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '网点类型：',
              codeField: 'orgType',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'orgType,orgTypeCn',
              customObj: {
                api: api.queryOrgTypes
              },
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '奖励金使用比例：',
              codeField: 'useRatio',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '状态：',
              codeField: 'isEnable',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'isEnable,isEnableCn',
              options: [
                { isEnable: '1', isEnableCn: '启用' },
                { isEnable: '0', isEnableCn: '禁用' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '备注：',
              codeField: 'remark',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'useRatioId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'controlBtn1',
              label: '操作',
              minWidth: 180,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '修改',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.handelEdit(index)
                }
              ]
            },
            {
              prop: 'orgTypeCn',
              label: '网点类型'
            },
            {
              prop: 'useRatio',
              label: '奖励金使用比例'
            },
            {
              prop: 'isEnableCn',
              label: '状态'
            },
            {
              prop: 'remark',
              label: '备注'
            },
            {
              prop: 'lastUpdatedDate',
              label: '最后修改时间'
            },
            {
              prop: 'modifier',
              label: '最后修改人'
            }
          ],

      // 表单查询数据
      formField: this.$utils.getFormField(this, {
        orgType: '',
        useRatio: '',
        isEnable: '',
        remark: '',
        updateControlId: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 保存
    save() {
      const that = this
      const queryObj = {
        // 保存需要传 type="mutation"
        type: 'mutation',
        apiConfig: api.saveBonusUseRatioSetting,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: that.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      this.$requestAPI(paramD).then((response) => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          // this.reset()
          this.queryTable(1)
        } else {
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }

        this.queryTable(1)
      })
    },
    handelEdit(index) {
      setDynamicFieldsData(this.formField, this.$refs.multipleTable.list[index])
    }
  },


  watch: {
    // 侦听搜索框输入的变化
    search(val) {
      this.$refs.tree.filter(val);
    }
  },



}
</script>
